using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using TddGenerator.Core.Domain;
using TddGenerator.Core.Services;
using TddGenerator.Core.Services.Impl;

namespace TddGenerator.UnitTests.Services
{
    [TestFixture]
    public class DefaultSettingsRepositoryTester
    {
        [Test]
        public void Repository_should_retrieve_default_settings()
        {
            MockRepository mocks = new MockRepository();
            IFileLocator fileLocator = mocks.CreateMock<IFileLocator>();
            ISerializer<DefaultSettings> serializer = mocks.CreateMock<ISerializer<DefaultSettings>>();
            IFileSystemService fileSystemService = mocks.CreateMock<IFileSystemService>();

            DefaultSettings settings = new DefaultSettings();
            using (mocks.Record())
            {
                Expect.Call(fileLocator.FileName).Repeat.Any().Return("locator.xml");
                Expect.Call(fileSystemService.FileExists("locator.xml")).Return(true);
                Expect.Call(serializer.Deserialize("locator.xml")).Return(settings);
            }

            using (mocks.Playback())
            {
                IDefaultSettingsRepository repository = new DefaultSettingsRepository(fileLocator, serializer, fileSystemService);
                DefaultSettings actualSettings = repository.Get();
                Assert.That(actualSettings, Is.EqualTo(settings));
            }

            mocks.VerifyAll();
        }

        [Test]
        public void Repository_should_save_settings_to_presistance()
        {
            MockRepository mocks = new MockRepository();
            IFileLocator fileLocator = mocks.CreateMock<IFileLocator>();
            ISerializer<DefaultSettings> serializer = mocks.CreateMock<ISerializer<DefaultSettings>>();
            IFileSystemService fileSystemService = mocks.CreateMock<IFileSystemService>();

            DefaultSettings settings = new DefaultSettings();

            using (mocks.Record())
            {
                Expect.Call(fileLocator.FileName).Repeat.Any().Return("locator.xml");                
                serializer.Serialize(settings,"locator.xml");
            }

            using (mocks.Playback())
            {
                IDefaultSettingsRepository repository = new DefaultSettingsRepository(fileLocator, serializer, fileSystemService);
                repository.Save(settings);                
            }

            mocks.VerifyAll();            
        }

    }
}